from odoo import fields, models, api, _
from odoo.exceptions import UserError, ValidationError


class OrderTypeSelectWizard(models.Model):
    _name = 'order.type.select.wizard'
    _description = 'Order Type Select Wizard'

    order_type = fields.Selection([
        ('new', 'NEW'),
        ('repeat', 'REPEAT'),
        ('change', 'CHANGE')
    ], string="Order Type", default='new', required=True)
    pn = fields.Char(default=lambda self: self.env.context.get('pn', False))
    partner_id = fields.Many2one('res.partner', default=lambda self: self.env.context.get('customer_id', False))
    crm_id = fields.Many2one('crm.lead', default=lambda self: self.env.context.get('crm_id', False))
    contact_id = fields.Many2one('res.partner', default=lambda self: self.env.context.get('contact', False))
    qty = fields.Integer(default=lambda self: self.env.context.get('qty', False))
    nre = fields.Integer(default=lambda self: self.env.context.get('nre', False))
    special_type = fields.Boolean(string="Special", default=lambda self: self.env.context.get('special_type', False))

    def confirmation(self):
        if not self.order_type:
            raise ValidationError(_("Please select an order type"))
        return {
            'name': _('Sale Place Order'),
            'type': 'ir.actions.act_window',
            'res_model': 'place.order.wizard',
            'view_mode': 'form',
            'target': 'new',
            'context': {
                'dialog_size': 'extra-large',
                'customer_id': self.partner_id.id,
                'pn': self.pn,
                'crm_id': self.crm_id.id,
                'contact_id': self.contact_id.id,
                'order_type': self.order_type,
                'qty': self.qty,
                'nre': self.nre if self.order_type == 'new' else 0,
                'special_type': self.special_type,
                's_simi_fg': self.env.context.get('s_simi_fg', False)
            },
        }
